﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using eCommMongo.Core.Domain.Products;
using eCommMongo.Service.Pictures;
using eCommMongo.Service.Products;
using eCommMongo.Web.Models.ProductVariants;

namespace eCommMongo.Web.Controllers
{



    public class ProductVariantController : Controller
    {
        private IProductService _productService;
        private IPictureService _pictureService;


        public ProductVariantController(
            IPictureService pictureService,
            IProductService productService)
        {
            _pictureService = pictureService;
            _productService = productService;
        }

        public ActionResult Create(string productId)
        {
            var model = new ProductVariantModel()
                {
                    ProductId = productId,
                    PicUrl = _pictureService.GetDefaultPictureUrl(100)

                };
            ViewBag.Model = model;
            return View();
        }

        public ActionResult Edit(string productVariantId, string tab)
        {

            var pv = _productService.GetProductVariantById(productVariantId);
            var model = new ProductVariantModel()
                {
                    ProductId = pv.ProductId,
                    Description = pv.Description,
                    DisplayOrder = pv.DisplayOrder,
                    Id = pv.Id,
                    Name = pv.Name,
                    PictureId = pv.Picture.Id,
                    Price = pv.Price,
                    Published = pv.Published,
                    Sku = pv.Sku,
                    StockQuantity = pv.StockQuantity,
                    ProductVariantAttributes = ToModelList(pv.Id,pv.ProductVariantAttributes),
                    AttributeControlTypes = GetAttributeControlTypes(),

                    PicUrl = _pictureService.GetPictureUrl(pv.Picture.Id, 100, false),
                };
            ViewBag.Model = model;

            ViewBag.Tab = string.IsNullOrEmpty(tab) ? "pvInfo" : tab;

            return View();
        }

        private IList<ProductVariantAttributeListModel> ToModelList(string pvId, ICollection<ProductVariantAttribute> pvAttributes)
        {
            return pvAttributes.Select(c => new ProductVariantAttributeListModel()
                {
                   Id= c.Id,
                   ProductAttributeName = c.ProductAttribute.Name,
                   AttributeControlTypeName = Enum.GetName(typeof(AttributeControlType),c.AttributeControlType)
                }).ToList();
        }

        private IList<AttributeControlTypeModel> GetAttributeControlTypes()
        {
            return null;
        }
    }
}
